package Client;

import Shared.ClientInt;
import Shared.User;
import java.awt.Dimension;
import static java.awt.SystemColor.window;
import java.awt.Toolkit;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class SignIn extends javax.swing.JFrame {

    String signin = "Images/signin.jpeg";
    String online = "Images/online.png";
    String offline = "Images/off.png";
    ClientImp client;
    ArrayList<User> friends = new ArrayList<>();
    int[] flag = new int[2];
    boolean fFlag = true;

    public SignIn() {
        try {
            client = new ClientImp();
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, "Server Is down", "Error From Server", JOptionPane.WARNING_MESSAGE);
            this.dispose();
            System.exit(0);
        }
        int frameWidth = 361;
        int frameHeight = 604;
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
        emailerror.setVisible(false);
        passerror.setVisible(false);
        signerror.setVisible(false);
        pass2error.setVisible(false);
    }

    //test
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelSignIn = new javax.swing.JPanel();
        email = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        signinButton = new javax.swing.JButton();
        emailText = new javax.swing.JTextField();
        passwordText = new javax.swing.JPasswordField();
        emailerror = new javax.swing.JLabel();
        passerror = new javax.swing.JLabel();
        signerror = new javax.swing.JLabel();
        pass2error = new javax.swing.JLabel();
        signupButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ChatUp(SignIn)");
        setResizable(false);

        panelSignIn.setBackground(new java.awt.Color(255, 255, 255));
        panelSignIn.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelSignIn.setForeground(new java.awt.Color(238, 242, 248));

        email.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 14)); // NOI18N
        email.setForeground(new java.awt.Color(41, 46, 76));
        email.setText("Email");

        password.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 14)); // NOI18N
        password.setForeground(new java.awt.Color(41, 49, 76));
        password.setText("Password");

        signinButton.setBackground(new java.awt.Color(102, 5, 51));
        signinButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 18)); // NOI18N
        signinButton.setForeground(new java.awt.Color(255, 255, 255));
        signinButton.setText("Sign In");
        signinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signinButtonActionPerformed(evt);
            }
        });

        emailText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                emailTextFocusLost(evt);
            }
        });

        passwordText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordTextActionPerformed(evt);
            }
        });
        passwordText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                passwordTextFocusLost(evt);
            }
        });

        emailerror.setFont(new java.awt.Font("DejaVu Sans", 0, 13)); // NOI18N
        emailerror.setForeground(new java.awt.Color(240, 27, 27));
        emailerror.setText("Email Not Valid");

        passerror.setFont(new java.awt.Font("DejaVu Sans", 0, 13)); // NOI18N
        passerror.setForeground(new java.awt.Color(240, 27, 27));
        passerror.setText("Password Not Valid");

        signerror.setForeground(new java.awt.Color(240, 27, 27));
        signerror.setText("*Missing Field(s) or invalid data");

        pass2error.setFont(new java.awt.Font("DejaVu Sans", 0, 13)); // NOI18N
        pass2error.setForeground(new java.awt.Color(240, 27, 27));
        pass2error.setText("*Wrong");

        signupButton.setBackground(new java.awt.Color(102, 0, 51));
        signupButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 18)); // NOI18N
        signupButton.setForeground(new java.awt.Color(255, 255, 255));
        signupButton.setText("Sign Up");
        signupButton.setPreferredSize(new java.awt.Dimension(83, 34));
        signupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signupButtonActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Client/Images/signin.jpeg"))); // NOI18N

        javax.swing.GroupLayout panelSignInLayout = new javax.swing.GroupLayout(panelSignIn);
        panelSignIn.setLayout(panelSignInLayout);
        panelSignInLayout.setHorizontalGroup(
            panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSignInLayout.createSequentialGroup()
                .addGap(0, 70, Short.MAX_VALUE)
                .addGroup(panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(password)
                    .addGroup(panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(signerror, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(signinButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(emailText, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(emailerror)
                        .addComponent(email)
                        .addGroup(panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(panelSignInLayout.createSequentialGroup()
                                .addComponent(passerror)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(pass2error))
                            .addComponent(passwordText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(signupButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(64, 64, 64))
            .addGroup(panelSignInLayout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelSignInLayout.setVerticalGroup(
            panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSignInLayout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(jLabel1)
                .addGap(54, 54, 54)
                .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(emailText, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(emailerror)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordText, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelSignInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passerror)
                    .addComponent(pass2error))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(signinButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(signerror)
                .addGap(18, 18, 18)
                .addComponent(signupButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelSignIn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelSignIn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void signinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signinButtonActionPerformed
        fFlag = true;

        for (int check : flag) {
            if (check == 0) {
                fFlag = false;
            }
        }
        if (fFlag) {
            signerror.setVisible(false);
            User user = new User();
            user.setUserMail(emailText.getText());
            user.setUserPass(new String(passwordText.getPassword()));
            friends = client.signinUser(user);
            MainForm mf = new MainForm(friends);
            client.setMainForm(emailText.getText(), mf);
            mf.setVisible(true);
            setVisible(false);
            dispose();
        } else {
            signerror.setVisible(true);
        }
    }//GEN-LAST:event_signinButtonActionPerformed

    private void emailTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emailTextFocusLost
        // TODO add your handling code here:
        if (emailText.getText().equals("")) {
            emailerror.setVisible(true);
            flag[0] = 0;
        } else {
            int checked = 0;
            try {
                checked = client.serverRef.checkMail(emailText.getText());
            } catch (RemoteException ex) {
                JOptionPane.showMessageDialog(null, "Server Is down", "Error From Server", JOptionPane.WARNING_MESSAGE);
            }
            // it return 1 if email doesn't exist
            if (checked == 0) {
                emailerror.setVisible(false);
                flag[0] = 1;
            } else {
                emailerror.setVisible(true);
                flag[0] = 1;
            }
        }
    }//GEN-LAST:event_emailTextFocusLost

    private void passwordTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordTextFocusLost
        // TODO add your handling code here:
        //System.out.println(passwordText.getPassword());
        String checkedPass = new String();
        if (passwordText.getPassword().equals("")) {
            passerror.setVisible(true);
            flag[1] = 0;
        } else {
            try {
                String passText = new String(passwordText.getPassword());
                System.out.println(passText);
                checkedPass = client.serverRef.checkPass(emailText.getText(), passText);
                if (passText.equals(checkedPass)) {
                    passerror.setVisible(false);
                    pass2error.setVisible(false);
                    flag[1] = 1;
                } else {
                    passerror.setVisible(true);
                    pass2error.setVisible(true);
                    flag[1] = 0;
                }
            } catch (RemoteException ex) {
                JOptionPane.showMessageDialog(null, "Server Is down", "Error From Server", JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_passwordTextFocusLost


    private void signupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signupButtonActionPerformed

        this.setVisible(false);
        new SignUp().setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_signupButtonActionPerformed

    private void passwordTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordTextActionPerformed
        // TODO add your handling code here:
        String checkedPass = new String();
        if (passwordText.getPassword().equals("")) {
            passerror.setVisible(true);
            flag[1] = 0;
        } else {
            try {
                String passText = new String(passwordText.getPassword());
                System.out.println(passText);
                checkedPass = client.serverRef.checkPass(emailText.getText(), passText);
                if (passText.equals(checkedPass)) {
                    passerror.setVisible(false);
                    pass2error.setVisible(false);
                    flag[1] = 1;
                } else {
                    passerror.setVisible(true);
                    pass2error.setVisible(true);
                    flag[1] = 0;
                }
            } catch (RemoteException ex) {
                Logger.getLogger(SignIn.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        fFlag = true;
        for (int check : flag) {
            if (check == 0) {
                fFlag = false;
            }
        }
        if (fFlag) {
            signerror.setVisible(false);
            User user = new User();
            user.setUserMail(emailText.getText());
            user.setUserPass(new String(passwordText.getPassword()));
            friends = client.signinUser(user);
            MainForm mf = new MainForm(friends);
            mf.setVisible(true);
            setVisible(false);
            dispose();
        } else {
            signerror.setVisible(true);
        }
    }//GEN-LAST:event_passwordTextActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SignIn().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel email;
    private javax.swing.JTextField emailText;
    private javax.swing.JLabel emailerror;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel panelSignIn;
    private javax.swing.JLabel pass2error;
    private javax.swing.JLabel passerror;
    private javax.swing.JLabel password;
    private javax.swing.JPasswordField passwordText;
    private javax.swing.JLabel signerror;
    private javax.swing.JButton signinButton;
    private javax.swing.JButton signupButton;
    // End of variables declaration//GEN-END:variables
}
